package com.dtease.agent.biz.agentstore.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dtease.agent.biz.agentstore.entity.BasToolsMetaDatabase;
import com.dtease.agent.biz.agentstore.mapper.ToolsMetaDatabaseMapper;
import com.dtease.agent.biz.agentstore.model.BasToolsMetaDataTable;
import com.dtease.agent.biz.agentstore.service.IBasToolsMetaDatabaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chentl
 * @since 2025-05-07
 */
@Service
public class BasBasToolsMetaDatabaseServiceImpl extends ServiceImpl<ToolsMetaDatabaseMapper, BasToolsMetaDatabase> implements IBasToolsMetaDatabaseService {

    @Transactional(transactionManager = "agentStoreTransactionManager", rollbackFor = Exception.class)
    @Override
    public void executeInTransaction(Runnable operation) {
        operation.run();
    }

    @Override
    public List<BasToolsMetaDataTable> listByTableGroup() {
        return this.baseMapper.listByTableGroup();
    }

    @Override
    public void updateTableTags(String tenantId, String storageId, String databaseName, String tableName, String value) {
        if(StringUtils.isAnyEmpty(tenantId, storageId, databaseName, tableName)){
            return;
        }
        lambdaUpdate()
                .eq(BasToolsMetaDatabase::getTenantId, tenantId)
                .eq(BasToolsMetaDatabase::getStorageId, storageId)
                .eq(BasToolsMetaDatabase::getDatabaseName, databaseName)
                .eq(BasToolsMetaDatabase::getTableName, tableName)
                .set(BasToolsMetaDatabase::getTableTags, value)
                .update();
    }
}
